package com.usc.blog.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.usc.blog.pojo.User;
import com.usc.blog.pojo.UserExample;
import com.usc.blog.pojo.UserExample.Criteria;
import com.usc.blog.service.UserService;

/**
 *
 * @版权: Copyright (c) 2016-2017 *********公司技术开发部
 * @author : huangjiapeng
 * @E-mail:1625545295@qq.com
 * @版本: 1.0
 * @创建日期: 2019年4月13日 下午1:18:55
 * @ClassName MainController
 * @类描述-Description: TODO
 * @修改记录:
 * @版本: 1.0
 */
@Controller
@RequestMapping("user")
public class UserLoginController {
	@Autowired
	UserService userService;

	@RequestMapping("/loginUI")
	public String getLoginUI(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "user/login";
	}

	// toLogin.do
	@RequestMapping("/toLogin")
	public String Login(HttpServletRequest request, HttpServletResponse response, Model model, User user,
			HttpSession session) {
		UserExample example = new UserExample();
		Criteria create = example.createCriteria();
		create.andUsernameEqualTo(user.getUsername());
		// create.andUseridEqualTo(user.getUserid());
		List<User> selectByExample = userService.selectByExample(example);
		System.out.println("!!!!!!!" + selectByExample.size());
		User u = null;
		if (selectByExample != null && selectByExample.size() != 0 && selectByExample.isEmpty() == false) {
			u = selectByExample.get(0);
			System.out.println(u.getPassword() + "密码" + u.getUsername());
		} else {
			request.setAttribute("msg1", "失败，账号不存在！");
			return "user/login";
		}
		if (u.getPassword().equals(user.getPassword())) {
			request.setAttribute("msg1", "登录成功");
			session.setAttribute("currentUser", u);
			// session.setAttribute("lists", newsLists);
			//
			// forward:listAll
			return "redirect:/data/listPage";
			// return "user/listPage";// 登录成功
		} else {
			// session.setAttribute("lists", newsLists);
			request.setAttribute("msg1", "失败;密码错误！");
			return "user/login";
		}
	}

	/**
	 * 注册 用户名
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @param user
	 * @param session
	 * @return
	 */

	@RequestMapping("/toRegister")
	public String toRegister(HttpServletRequest request, HttpServletResponse response, Model model, User user,
			String yzm, HttpSession session) {
		UserExample example = new UserExample();
		Criteria create = example.createCriteria();
		create.andUsernameEqualTo(user.getUsername());
		List<User> selectByExample = userService.selectByExample(example);
		if (selectByExample.size() == 0) {
			// 账号不存在
			Date now = new Date();
			user.setCreatedate(now);
			user.setUpdatetime(now);
			user.setStatus(1);
			user.setNikename(user.getUsername());
			int insert = userService.insert(user);
			// 成功返回1
			if (insert != 0) {
				request.setAttribute("msg2", "注册成功");
			} else {
				request.setAttribute("msg2", "注册失败");
			}
		} else {
			// 账号已经存在
			request.setAttribute("msg2", "账号存在");
		}
		return "user/login";
	}

	@RequestMapping("/toLogout")
	public String toLogout(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		// 注销 删除所有的session
		session.invalidate();
		// 进入新闻首页 重定向
		// return "redirect::index.do";
		return "redirect:/data/listPage";
	}
}
